package info.ask4research.web;

import info.ask4research.model.User;
import info.ask4research.service.impl.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class LoginController {

	private UserService userService;
	
	
	@RequestMapping(value="jsp/login/login.action")
	public @ResponseBody Map login(
			@RequestParam(value="username") String username,
			@RequestParam(value="password") String password,
			HttpServletRequest request,
            HttpServletResponse response,
            HttpSession session) throws Exception {
			
			
		
			
				//request.setAttribute("username", username);
		List<User> list = userService.getUserByUsernameAndPassword(username, password);
			if(list.size()>0 && StringUtils.isNotEmpty(list.get(0).getUsername())){
				Map<String,Object> modelMap = new HashMap<String,Object>(3);
				modelMap.put("total", 1);
				modelMap.put("data", "data");
				modelMap.put("success", true);
				modelMap.put("user", list.get(0));
				session.setAttribute("loginId", list.get(0).getId());
				session.setMaxInactiveInterval(10800);
			return modelMap;
			}else{
				Map<String,Object> modelMap = new HashMap<String,Object>(3);
				modelMap.put("total", 1);
				modelMap.put("data", "data");
				modelMap.put("success", false);
				modelMap.put("user", username);
				return modelMap;
			}

		
	}
	
	@RequestMapping(value="jsp/login/register.action")
	public @ResponseBody Map register(
			@RequestParam(value="firstName") String firstName,
			@RequestParam(value="lastName") String lastName,
			@RequestParam(value="email") String email,
			@RequestParam(value="username") String username,
			@RequestParam(value="password") String password) throws Exception {
			
			
				User user = new User();
				user.setEmail(email);
				user.setFirstName(firstName);
				user.setLastName(lastName);
				user.setUsername(username);
				user.setPassword(password);
				userService.save(user);
				Map<String,Object> modelMap = new HashMap<String,Object>(3);
				modelMap.put("total", 1);
				modelMap.put("data", user);
				modelMap.put("success", true);
				return modelMap;

			

		
	}

	
	/**
	 * Generates modelMap to return in the modelAndView
	 * @param contacts
	 * @return
	 */
	private Map<String,Object> getMap(List<User> users){
		
		Map<String,Object> modelMap = new HashMap<String,Object>(3);
		modelMap.put("total", users.size());
		modelMap.put("data", users);
		modelMap.put("success", true);
		
		return modelMap;
	}
	
	/**
	 * Generates modelMap to return in the modelAndView in case
	 * of exception
	 * @param msg message
	 * @return
	 */
	private Map<String,Object> getModelMapError(String msg){

		Map<String,Object> modelMap = new HashMap<String,Object>(2);
		modelMap.put("message", msg);
		modelMap.put("success", false);

		return modelMap;
	} 


	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
